package com.service;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.domain.GoodsDetail;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.entity.OrdersEntity;
import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.XSlf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import static com.alipay.api.AlipayConstants.FORMAT;

/**
 * @author zooy
 * @since 2024/6/26-22:14
 */
@Service
@Slf4j
public class PayService {

    public String unifiedOrder(OrdersEntity ordersEntity) {

        AlipayClient alipayClient = getAlipayClient();

        // 构造请求参数以调用接口
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();

        // 设置商户门店编号
        model.setStoreId("NJ_001");

        // 设置订单标题
        model.setSubject("travel line 001");

        // 设置请求来源地址
        model.setRequestFromUrl("http://localhost:8080/springbootmt74k/front/index.html");

        // 设置产品码
        model.setProductCode("FAST_INSTANT_TRADE_PAY");

        // 设置PC扫码支付的方式
        // 走跳转模式
        model.setQrPayMode("2");

        // 设置订单绝对超时时间
        String expiryTime = LocalDateTime.now().plusMinutes(5).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        model.setTimeExpire(expiryTime);

        // 设置商户自定义二维码宽度
        model.setQrcodeWidth(100L);

        // 设置请求后页面的集成方式
        model.setIntegrationType("PCWEB");

        // 设置商户订单号
        model.setOutTradeNo(ordersEntity.getOrderid());

        // 设置订单总金额
        model.setTotalAmount(String.valueOf(ordersEntity.getTotal()));

        request.setBizModel(model);

        AlipayTradePagePayResponse response = null;
        try {
            response = alipayClient.pageExecute(request, "POST");

            if (response.isSuccess()) {
                System.out.println("调用成功");
                String pageRedirectionData = response.getBody();
                return pageRedirectionData;
            } else {
                System.out.println("调用失败");
                // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
                // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
                // System.out.println(diagnosisUrl);
            }
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }

        return null;

    }

    public AlipayClient getAlipayClient() {

        AlipayConfig alipayConfig = new AlipayConfig();
        //设置沙箱网关地址
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        //设置应用ID
        alipayConfig.setAppId("9021000138654660");
        //设置应用私钥
        alipayConfig.setPrivateKey("MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCD6Bg9ZDT/nZ+YYuzXEh8o+/2LRERTSUi/Tg8C3vLDHcKipDWb7DePc6NO7u6YZHYxHgOMY0Xc32/dwCIwG5PwU9q/+GXhZQxNrLolJ3gkAb5z19V5ksDLsX2EWIZECVqohO45Fa1+62Qw9pKWdJjNBLBNiYnQlTGCv1tGDOpTgrje0zNKtEJCaSQ4oc8E0R0SGxSD+9Av0mrVmCuDjqxCnkM9A9ja/dkXHM7hmT/qliGYMLHgPiqs/H81bQtst/UnCIpi4FM6rpUtx8DP1+JZm+qDSYsHMxsPZ5ZBmYoGcQaI3/FUCldPHo1K6f2in6H2OBYNfBTF+u55N+wvxCSrAgMBAAECggEAaoW7VZCgEDF9mfWBqGm1Ckb9yUL3Hn9XJU7nZn0fdRyKewe7V1DayZksL+lae2zd2eqQGQZue+FhGKY5tbn/rZw6Ur9Jn00YM34i0TBfoExcPnb1qXkvAxAx+ff3behT8zVuoGD2PEHiJnxpguFkKGT575P73JsvYZQhJJ/3eDosvOF5UgQm1PzDF3hHjtl7sFEGJjWiXp8l07NMdlanQUC6hT0O0gcvBb9RKxFP7XyVoPCgZDpXfAcpW8RLA9UFtbME7Ly55jO8hq6E03tGF8QUC1iQIqEY6eUQVbxonWrAjeeJzpZRAsUQYdsjHuLAbquciA5pCU1cjVOBjRFouQKBgQDvmCtStKs3G5hyVHgN5IhG6Jh6l+OoZSKz/oy2UpQqFgSQqjiokXcmO8tGjXCrO7DJDwXF+PtY81MdBaIjOa1iAgRV6s4uQbnfaZZQQzXWo2rKTfnlfw8lLl6W66cjLREIwvt1VWL1690N7alCMM1A6KmiO6mH67QzkhoUDO0uNQKBgQCM8EZmgbP3OO1R8BqcCP057oiaDT0fQM3ircUN0OUo6D+TNdK4N5SsL9Ciu3S0v7T5lk8Diusih+tYI/j7F5w9Sx56CVLnhe2cTprIbvPVFj1zlQc6oyPBMu5amhKpvwLoVkIXjtciMQNiaj4cU1CzmErDEjkXDE8DBQdMqYvjXwKBgQCghY7hBnJ/pVlpn/YfOLGk8EjYOdMpCzlI5o5XyNPDrqS4V5NGPM9bKkxypGiMEJ3M1ufDT6D2Y2+XziJm/0mOlFNbTMl+bEKf0BoJBD6Z4zq097EpdQssmfh9nLyJ8LOs7xWR8vcuVCvyHPx9gDzD9B1SuC5JBj3hH02hiVeO0QKBgBqUCUK1fQHHO4HzPKFmVJLDOAGNM5ScPPdde8bCOSUAv1R8lvNkj+X00myN21FmwbM7tETfwr3tUeV6Sy7W4gDxFJtA/OBqckfCzw+zVMo/enE8jnNxZoG9oBN38MMQIXU9JxOYPVvWUGQT0wi2cYac1qR44ElHafMQ1LKRWq8TAoGAK6rraFYIhxQeSYHRryCG2w2pyaUuvve5A+8qY6ZYZmmlJvEGvRAPUII37JYCH+Tw2e4A8ogRbpGUB756hoCMR88nWNlidoUTFTi/yFdNNTmivO7D+aD/F4weOopgxZTiGQEbTrxbaiYlekwl0fuUFUkLAjErMddpLWo+F3ZPAG0=");
        //设置请求格式，固定值json
        alipayConfig.setFormat("json");
        //设置字符集
        alipayConfig.setCharset("utf-8");
        //设置签名类型
        alipayConfig.setSignType("RSA2");
        //设置支付宝公钥
        alipayConfig.setAlipayPublicKey("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAn21dUg9f7uT64zxP3d5DGJUbIPLMpOMHRUd+tc2H0GZAcZvpqi1hbVhYxBfs3gL6rFJ2p5K0zd5dr6jYtkyJbDJMWj9M6jDLVHoCYXhPn8GQEJzQp7/WgUpCRs64lkU0QgG12/jg/7L7Vf4zSzI0A6l7c42l0hQelF/813SYz1x+xnS+meCC58RDRhwA+Sjz1EsGj/Lg1OlSMenAK82qaSpJxPjPQk2E4gGKZyaQxWHz3rRwSsy2P+zt51VLZW8Jio5eOoPbrV2I7qvXGEe0166b02DDwgEXAVfqZfAJa9rw7FtN/PLyUBnbnlEZ8OuEnpBOUxTjZd3xZwpTxchXIQIDAQAB");
        //实例化客户端
        AlipayClient alipayClient = null;
        try {
            alipayClient = new DefaultAlipayClient(alipayConfig);
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }

        return alipayClient;
    }
}
